home *** CD-ROM | disk | FTP | other *** search
/ HyperLib 1997 Winter - Disc 1 / HYPERLIB-1997-Winter-CD1.ISO.7z / HYPERLIB-1997-Winter-CD1.ISO / オンラインウェア / PRG / PICTButton CDEF 1.3 folder.sit / PICTButton CDEF 1.3 folder / PICTButton CDEF 1.3 / PICTButton.c < prev    next >
C/C++ Source or Header  |  1996-01-04  |  6KB  |  242 lines

  1. /* ----------------------------------------------------------------------
  2.  
  3.     PICTButton CDEF
  4.     version 1.3
  5.     
  6.     Written by: Paul Celestin
  7.     
  8.     Copyright ゥハ1993-1995 Celestin Company, Inc.
  9.     
  10.     This CDEF displays a picture whose resource ID is derived from
  11.     the min, max, and value fields of the CNTL. For a multiple-state
  12.     button, set min to ID of the the first PICT and max to the ID of
  13.     the last PICT. Also set value to the ID of the first PICT.
  14.     
  15.     930822 - 1.0.0    initial release
  16.     930829 - 1.0.1     added variations 1 and 2
  17.     930915 - 1.0.2    fixed the new variations
  18.     930922 - 1.0.3    fixed a problem with black and white PICTs showing
  19.                     up, or so I thought
  20.     931012 - 1.0.4    changed color of text in variation 2 to blue
  21.     931012 - 1.0.5    really fixed problem with black and white PICTs
  22.                     showing up properly
  23.     940319 - 1.0.6    removed unnecessary erasing code
  24.     940703 - 1.0.7    now works over multiple monitors of different bit
  25.                     depths
  26.     950804 - 1.1.0  finally converted this thing over to CodeWarrior!
  27.     950809 - 1.2.0  fixed bug in hasColor that caused computer to crash
  28.                     if button was partially or completely offscreen
  29.     951215 - 1.3.0  updated to CW7
  30.  
  31. ---------------------------------------------------------------------- */
  32.  
  33. #define kPlain            0
  34. #define kInverted        1
  35. #define kInvertedPICT    100
  36. #define kOffset            200
  37. #define kIsColorPort     0xC000 // If these bits are set, its a CGrafPort.
  38.  
  39.  
  40. /* ----------------------------------------------------------------------
  41. prototypes
  42. ---------------------------------------------------------------------- */
  43. pascal long main(short variation, ControlHandle theControl, short message, long param);
  44. int hasColor(Rect);
  45. void drawIt(ControlHandle control,short variation);
  46. long testIt(ControlHandle control, Point myPoint);
  47.  
  48.  
  49. /* ----------------------------------------------------------------------
  50. main
  51. ---------------------------------------------------------------------- */
  52. pascal long main(short variation, ControlHandle theControl, short message, long param)
  53. {
  54.     long    returnValue = 0L;
  55.     char    state = HGetState((Handle)theControl);
  56.  
  57.     switch(message)
  58.     {
  59.         case drawCntl:
  60.             drawIt(theControl,variation);
  61.         case testCntl:
  62.             returnValue = testIt(theControl, *(Point *) ¶m);
  63.         case calcCRgns:
  64.             break;
  65.           case initCntl:
  66.               break;
  67.         case dispCntl:
  68.             break;
  69.         case posCntl:
  70.             break;
  71.         case thumbCntl:
  72.             break;
  73.         case dragCntl:
  74.             break;
  75.         case autoTrack:
  76.             break;
  77.         case calcCntlRgn:
  78.             break;
  79.         case calcThumbRgn:
  80.             break;
  81.         default:
  82.             break;
  83.     }
  84.  
  85.     HSetState((Handle)theControl,state);
  86.  
  87.     return(returnValue);                /* tell them what happened */
  88. }
  89.  
  90.  
  91. /* ----------------------------------------------------------------------
  92. hasColor
  93. ---------------------------------------------------------------------- */
  94. int hasColor(Rect r)
  95.  
  96. {
  97.     SysEnvRec        myComputer;
  98.     GDHandle        curDev;
  99.     PixMapHandle    myPixMap;
  100.     
  101.     SysEnvirons(2,&myComputer);
  102.     if (myComputer.hasColorQD)
  103.     {
  104.         LocalToGlobal((Point*) &r);
  105.         LocalToGlobal(1 + (Point*) &r);
  106.         curDev = GetMaxDevice(&r);
  107.         if (curDev != nil)
  108.         {
  109.             myPixMap = (**curDev).gdPMap;
  110.             if ((**myPixMap).pixelSize > 1)
  111.                 return(1);
  112.             else
  113.                 return(0);
  114.         }
  115.     }
  116.     return(0);
  117. }
  118.  
  119.  
  120. /* ----------------------------------------------------------------------
  121. drawIt - here is where we actually draw the control
  122. ---------------------------------------------------------------------- */
  123. void drawIt(ControlHandle control, short variation)
  124.  
  125. {
  126.     short                invert = 0, useBW = 0;
  127.     int                    savedFont, savedSize, savedMode;
  128.     Rect                 myRect;
  129.     GrafPtr                thePort;
  130.     PicHandle            myPicture;
  131.     PenState            oldPenState;
  132.     Str255                myTitle;
  133.     Pattern                myGray;
  134.  
  135.     GetPort(&thePort);                            /* save off the current port */
  136.  
  137.     if (!(*control)->contrlVis)                    /* if not visible, do nothing */
  138.         return;
  139.  
  140.     myRect = (*control)->contrlRect;
  141.  
  142.     if (!hasColor(myRect))                        /* use the B&W PICTs */
  143.         useBW = kOffset;
  144.     
  145.     if ((*control)->contrlHilite == inButton)
  146.     {
  147.         invert = kInvertedPICT;                    /* invert while tracking */
  148.         SetControlReference(control, kInverted);
  149.     }
  150.     else
  151.     {
  152.         if ( (GetControlReference(control) == kInverted) && (!Button()) )
  153.         {
  154.             SetControlReference(control, kPlain);
  155.             if (GetControlValue(control) == GetControlMaximum(control))
  156.                 SetControlValue(control,GetControlMinimum(control));
  157.             else
  158.                 SetControlValue(control,GetControlValue(control) + 1);
  159.         }
  160.     }
  161.  
  162.     myPicture = (PicHandle)GetResource('PICT', (GetControlValue(control) + invert + useBW));
  163.     
  164.     if ( myPicture == 0L )                        /* could not find the PICT */
  165.         return;
  166.  
  167.     LoadResource((Handle)myPicture);
  168.     HNoPurge((Handle)myPicture);
  169.  
  170.     DrawPicture(myPicture, &myRect);            /* draw the picture */
  171.     
  172.     switch (variation)
  173.     {
  174.     case 1:                                        /* display title of control in geneva 9 */
  175.         {
  176.             savedFont = thePort->txFont;        /* save off current values */
  177.             savedSize = thePort->txSize;
  178.             savedMode = thePort->txMode;
  179.             
  180.             ForeColor(blueColor);
  181.             TextFont(geneva);
  182.             TextSize(9);
  183.             TextMode(srcOr);
  184.             
  185.             BlockMove(((*control)->contrlTitle),myTitle,((*control)->contrlTitle)[0] + 1);
  186.             
  187.             MoveTo((myRect.right+myRect.left) / 2 - StringWidth(myTitle) / 2, myRect.bottom - 6);
  188.             DrawString(myTitle);
  189.             
  190.             TextFont(savedFont);                /* restore saved values */
  191.             TextSize(savedSize);
  192.             TextMode(savedMode);
  193.             ForeColor(blackColor);
  194.             
  195.             break;
  196.         }
  197.     case 2:                                        /* simple 2-pixel wide black border */
  198.         {
  199.             PenSize(2,2);
  200.             InsetRect(&myRect,-3,-3);
  201.             FrameRect(&myRect);
  202.             InsetRect(&myRect,3,3);
  203.             PenSize(1,1);
  204.  
  205.             break;
  206.         }
  207.     }
  208.  
  209.     if ((*control)->contrlHilite == 255)        /* gray out the picture */
  210.     {
  211.         GetPenState(&oldPenState);
  212.         PenNormal();
  213.         GetIndPattern(&myGray,0,4);
  214.         PenPat(&myGray);
  215.         PenMode(patBic);
  216.         PaintRect(&myRect);
  217.         SetPenState(&oldPenState);
  218.     }
  219.     
  220.     HPurge((Handle)myPicture);
  221.     
  222.     return;                                        /* we are done drawing */
  223. }
  224.  
  225.  
  226. /* ----------------------------------------------------------------------
  227. testIt - test for mouse hits within control
  228. ---------------------------------------------------------------------- */
  229. long testIt(ControlHandle control, Point myPoint)
  230.  
  231. {
  232.     Rect myRect;
  233.  
  234.     myRect = (*control)->contrlRect;
  235.     
  236.     if    (((*control)->contrlVis != 0) && ((*control)->contrlHilite != 255) &&
  237.             (PtInRect(myPoint,&myRect)))
  238.         return(inButton);
  239.     else
  240.         return(0);
  241. }
  242.